﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class HoaDonDAO : ConnectionDAO
    {
        public static bool LapHD(long mHD, long mKH, DateTime nLHD)
        {
            OleDbConnection cN = null;
            bool temp = true;

            try
            {
                cN = Cnect();

                string cmString = @"INSERT INTO HOADON(MaHoaDon,KHLapHD,NgayLapHD) VALUES (@mHD,@mKH,@nLHD)";

                OleDbCommand cM = new OleDbCommand(cmString, cN);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@mHD", OleDbType.Integer);
                thamSo.Value = mHD;
                cM.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@mKH", OleDbType.Integer);
                thamSo.Value = mKH;
                cM.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@nLHD", OleDbType.Date);
                thamSo.Value = nLHD;
                cM.Parameters.Add(thamSo);

                cM.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                temp = false;
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            return temp;

        }

        public static long UpdateTriGia(long mHD, double tG)
        {
            long temp = 1;
            OleDbConnection cN = null;

            try
            {
                cN = Cnect();

                string cmString = @"UPDATE HOADON
                                    SET TriGia=@tG
                                    WHERE MaHoaDon=@mHD";

                OleDbCommand cM = new OleDbCommand(cmString, cN);

                OleDbParameter thamSo;






                thamSo = new OleDbParameter("@tG", OleDbType.Double);
                thamSo.Value = tG;
                cM.Parameters.Add(thamSo);


                thamSo = new OleDbParameter("@mHD", OleDbType.Integer);
                thamSo.Value = mHD;
                cM.Parameters.Add(thamSo);

                cM.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                temp = 0;
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            return temp;
        }

        public static bool KtMaHD(long mHD)
        {
            OleDbConnection cN = null;
            long t = 0;

            try
            {
                cN = Cnect();

                string cmString = @"SELECT HD.MaHoaDon
                                    FROM HOADON HD
                                    WHERE HD.MaHoaDon=@mHD";

                OleDbCommand cM = new OleDbCommand(cmString, cN);

                OleDbParameter thamSo = new OleDbParameter("@mHD", OleDbType.Integer);
                thamSo.Value = mHD;
                cM.Parameters.Add(thamSo);

                OleDbDataReader rDer = cM.ExecuteReader();


                while (rDer.Read())
                {
                    if (!rDer.IsDBNull(0))
                        t = rDer.GetInt32(0);
                    else
                        t = 0;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (cN != null && cN.State == System.Data.ConnectionState.Open)
                    cN.Close();
            }

            if (t == 0)
            {
                return true;
            }
            return false;
        }
    }
}
